package com.sqlist.share.resolver;

import android.util.Log;

import com.sqlist.share.dto.MessageDTO;
import com.sqlist.share.resolver.impl.LinkDTODataResolver;
import com.sqlist.share.resolver.impl.LocationDTODataResolver;
import com.sqlist.share.util.GsonUtils;

import java.util.concurrent.ConcurrentHashMap;

import io.netty.channel.Channel;

/**
 * Created by SqList on 2018/6/13.
 */

public class DataResolverProxy {
    private static final String TAG = "Share - DataResolverProxy";

    private static final DataResolverProxy INSTANCE = new DataResolverProxy();

    public static DataResolverProxy getSingleton() {
        return INSTANCE;
    }

    private final ConcurrentHashMap<String, DataResolver> RESOLVER_MAP = new ConcurrentHashMap<>();

    public DataResolverProxy() {
        RESOLVER_MAP.put("linkDTO", new LinkDTODataResolver());
        RESOLVER_MAP.put("locationDTO", new LocationDTODataResolver());
    }

    public void doAction(String messageString, Channel channel) {
        Log.i(TAG, "doAction " + messageString);

        MessageDTO messageDTO = GsonUtils.getInstance().fromJson(messageString, MessageDTO.class);
        RESOLVER_MAP.get(messageDTO.getType()).resolve(messageString, channel);
    }
}
